package com.demo.controller;

import com.demo.pojo.Admin;
import com.demo.pojo.User;
import com.demo.service.AdminService;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserController {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    @Autowired
    @Qualifier("adminServiceImpl")
    private AdminService adminService;

    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }

    @RequestMapping(value = "/login")
    public String login(User user, Model model, HttpServletRequest httpServletRequest){
        User user2 = userService.selectUserByName(user.getUsername());
        if(user2!=null&&user.getUserpassword().equals(user2.getUserpassword())){
            httpServletRequest.getSession().setAttribute("user",user2);
            return "user_main";
        }
        Admin admin = adminService.selectAdminByName(user.getUsername());
        if(admin!=null&&admin.getAdminpassword().equals(user.getUserpassword())){
            httpServletRequest.getSession().setAttribute("admin",admin);
            return "admin_main";
        }
        model.addAttribute("loginmsg", "用户名或密码错误");
        return "login";
    }

    @RequestMapping("/tousermain")
    public String tousermain(){
        return "user_main";
    }

    @RequestMapping("/")
    public String index(){
        return "login";
    }

    @RequestMapping("/toupdate")
    public String toupdate(){
        return "update";
    }

    @RequestMapping("/update")
    public String update(String userpassword, String tel, Model model, HttpServletRequest httpServletRequest){

        User newuser = (User) httpServletRequest.getSession().getAttribute("user");

        if(userpassword!=null && !userpassword.equals("")){
            newuser.setUserpassword(userpassword);
        }
        if(tel!=null && !tel.equals("")){
            newuser.setTel(tel);
        }
        int i = userService.updateUser(newuser);
        if(i>=1){
            model.addAttribute("updatemsg", "修改成功");
        }
        else {
            model.addAttribute("updatemsg", "修改失败");
        }

        return "update";
    }

    @RequestMapping("/querymoney")
    public String querymoney(HttpServletRequest httpServletRequest){
        User user = (User) httpServletRequest.getSession().getAttribute("user");
        User user1 = userService.selectUserByName(user.getUsername());
        httpServletRequest.getSession().setAttribute("user", user1);
        return "query_money";
    }

    @RequestMapping("/totransfermoney")
    public String totransfermoney(){
        return "transfer_money";
    }

    @RequestMapping("/transfermoney")
    public String transfermoney(String tousername, String money, Model model, HttpServletRequest httpServletRequest){
        User user = (User) httpServletRequest.getSession().getAttribute("user");
        int intmoney = Integer.parseInt(money);
        User touser = userService.selectUserByName(tousername);
        if (touser == null) {
            model.addAttribute("msg", "该账户不存在。");
            return "transfer_money";
        } else if (touser.getIsCold() == 1) {
            model.addAttribute("msg", "该账户处于冻结状态，无法汇款。");
            return "transfer_money";
        } else if (user.getBalance() < intmoney) {
            model.addAttribute("msg", "抱歉，您的余额不足。");
            return "transfer_money";
        }
        user.setBalance(user.getBalance() - intmoney);
        touser.setBalance(touser.getBalance() + intmoney);
        int i = userService.updateUser(user);
        int j = userService.updateUser(touser);
        if (i >= 1 && j >= 1) {
            model.addAttribute("msg", "转账成功！");
            httpServletRequest.getSession().setAttribute("user", user);
        } else {
            model.addAttribute("msg", "转账失败！");
        }

        return "transfer_money";
    }

    @RequestMapping("/verify")
    public String verify(Model model, HttpServletRequest httpServletRequest){
        User user = (User) httpServletRequest.getSession().getAttribute("user");
        if (user.getBalance()<0){
            model.addAttribute("verifymsg","数据异常，已重置！");
            return "verify";
        }
        model.addAttribute("verifymsg","校验成功，数据正常");
        return "verify";
    }

    @RequestMapping("/logoff")
    public String logoff(HttpServletRequest httpServletRequest){
        httpServletRequest.getSession().setAttribute("user", null);
        httpServletRequest.getSession().setAttribute("admin",null);
        return "login";
    }

    @RequestMapping("/torevoke")
    public String torevoke(){
        return "revoke";
    }

    @RequestMapping("/revoke")
    public String revoke(String tel, HttpServletRequest httpServletRequest, Model model, HttpServletResponse httpServletResponse){
        User user = (User) httpServletRequest.getSession().getAttribute("user");
        if (!tel.equals(user.getTel())){
            model.addAttribute("revokemsg", "电话错误，身份验证失败。");
            return "revoke";
        }
        if(user.getBalance()>0){
            model.addAttribute("revokemsg", "您的账户余额不为0，无法注销。");
            return "revoke";
        }
        int i = userService.deleteUser(user.getUserid());
        if(i>=1){
            return "redirect:/tologin";
        }
        else{
            model.addAttribute("revokemsg", "注销失败。");
            return "revoke";
        }
    }

    @RequestMapping("/tocold")
    public String tocold(){
        return "colduser";
    }

    @RequestMapping("/cold")
    public String cold(HttpServletRequest httpServletRequest,Model model){
       User user = (User) httpServletRequest.getSession().getAttribute("user");
       if (user.getIsCold()==1){
           user.setIsCold(0);
           userService.updateUser(user);
           model.addAttribute("coldmsg", "解冻成功，相关操作可进行。");
       }
       else {
           user.setIsCold(1);
           userService.updateUser(user);
           model.addAttribute("coldmsg", "冻结成功，相关操作已禁止。");
       }
        return "colduser";
    }

    @RequestMapping("/toregister")
    public String toregister(){
        return "register";
    }

    @RequestMapping("/register")
    public String register(User user,Model model){
        if (userService.selectUserByName(user.getUsername())!=null){
            model.addAttribute("registermsg", "此用户名已被注册");
            return "register";
        }
        int i = userService.addUser(user);
        if(i==0){
            model.addAttribute("registermsg", "开户失败");
            return "register";
        }
        else {
            model.addAttribute("loginmsg", "开户成功");
            return "login";
        }
    }
}
